<?php

namespace app\admin\service;

use app\common\model\trInvoices;
use app\common\model\trOrder;
use app\common\model\trUser;
use app\common\util\ErrorCode;

/**
 * Class Auth
 * @package app\admin\service
 * 发票服务类
 */
class InvoiceService
{
    private $field = [
        "user_id" => "require", "order_sn" => "", "type" => 1, "head_name" => "require", "duty_number" => "",
        "money" => "require", "mobile" => "", "address" => ""
    ]; //填入数据库需要存储的表字段
    private $data = [];
    private $returnData;
    private $mall_id = 0;
    private $params;

    public function __construct($data, $params = [])
    {
        $this->returnData = $data;
        $this->params = $params;
        $this->mall_id = request()->mallData["id"];
    }

    // 获取发票数据的分页列表
    public function getList(){
        $page = isset($this->params['page']) ? $this->params['page'] : 1;
        $pageSize = isset($this->params['pageSize']) ? $this->params['pageSize'] : 10;
        //搜索条件
        $filter = array(["mall_id", "=", $this->mall_id]);
        if(!empty($this->params['search'])){
            if(is_numeric($this->params["search"])){ //订单编号
                $filter[] = ['order_sn', '=', $this->params["search"]];
            }else{
                $userModel = new trUser();
                $filter[] = ['user_id', 'in', $userModel->getValue("id", [["nickname", "like", "{$this->params["search"]}%"]])];
            }
        }
        if(!empty($this->params['type'])){
            $filter[] = ['type', '=', $this->params["type"]];
        }
        if(!empty($this->params['status'])){ // 状态：1未开票；2已开票；3已拒绝
            $filter[] = ['status', '=', $this->params["status"]];
        }
        $model = new trInvoices();
        $data = $model->getList("*", $filter, $page, $pageSize);
        if(!$data["data"]){
            $this->returnData['data'] = ['list' => [], 'totalPage' => $page, 'currentPage' => $page, 'count' => 0];
        }else {
            $this->returnData['data'] = ['list' => $data['data'], 'totalPage' => $data['last_page'], 'currentPage' => $data['current_page'], 'count' => $data['total']];
        }
        return $this->returnData;
    }

    // 添加发票
    public function toAdd(){
        foreach ($this->field as $k => $v) {
            $ks = parse_name($k); //转换字段，变驼峰
            if($v === 'require' && !isset($this->params[$ks])){
                return ErrorCode::code("params_not_exist", $ks);
            }
            $this->data[$k] = isset($this->params[$ks]) ? $this->params[$ks] : $v;
        }
        $model = new trInvoices();
        $result = $model->add($this->data, true);
        if(isset($result["code"])) {
            return $result;
        }
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        if(isset($this->params["order_sn"])) {
            $orderModel = new trOrder();
            $orderModel->doUpdate(["invoice" => $result], ["order_sn" => $this->params["order_sn"]]);
        }
        return $this->returnData;
    }

    // 发票的详细内容
    public function getDetail(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        $model = new trInvoices();
        $data = $model->getFields("*", ["id" => $this->params["id"], "mall_id" => $this->mall_id]);
        $this->returnData["data"] = $data;
        return $this->returnData;
    }

    // 更新状态
    public function toUpdateStatus(){ //状态：1未开票；2已开票；3已拒绝
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        if(!isset($this->params["status"])){
            return ErrorCode::code("params_not_exist", "status");
        }
        if(!in_array($this->params["status"], [1,2,3])){
            return ErrorCode::code("params_error", "status(1未开票；2已开票；3已拒绝)");
        }
        $this->data["status"] = $this->params["status"];

        $model = new trInvoices();
        $result = $model->doUpdate($this->data, ["id" => $this->params["id"], "mall_id" => $this->mall_id]);
        if(isset($result["code"])) {
            return $result;
        }
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    // 删除发票
    public function toDel(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        $model = new trInvoices();
        $result = $model->delById($this->params['id']);
        if (isset($result["code"])) {
            return $result;
        }
        if (!$result) {
            $this->returnData = ErrorCode::$errorCode['fail'];
            $this->returnData["msg"] = "删除失败";
        }
        return $this->returnData;
    }
}